//package com.xqq.task;
//
//
//import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.xqq.dao.ScheduleTaskDAO;
//import com.xqq.dao.WeatherDAO;
//import com.xqq.dao.WeatherPointDAO;
//import com.xqq.entity.ScheduleTaskDO;
//import com.xqq.entity.WeatherDO;
//import com.xqq.entity.WeatherPointDO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.ApplicationArguments;
//import org.springframework.boot.ApplicationRunner;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.time.LocalDateTime;
//import java.util.List;
//import java.util.concurrent.CompletionService;
//import java.util.concurrent.ExecutorCompletionService;
//
//@Component
//@Slf4j
//public class weatherTask {
//
//    @Resource
//    private WeatherPointDAO weatherPointDAO;
//
//    @Resource
//    private WeatherDAO weatherDAO;
//
//    @Resource
//    private ScheduleTaskDAO scheduleTaskDAO;
//
//    @Value("${WEATHER_API_KEY}")
//    private String weatherApiKey;
//
//    public static final String SCHEDULE_WEATHER_TASK = "weatherTask";
//
//    //00:01 开始执行，每3小时执行一次
//    @Scheduled(cron = "0 1 * * * ?") // 每小时的第 1 分钟执行
//    public void saveWeatherTask() {
//        System.out.println("定时任务执行开始时间: " + new java.util.Date());
//        ScheduleTaskDO scheduleTaskDO = scheduleTaskDAO.lambdaQuery()
//                .eq(ScheduleTaskDO::getTaskName, SCHEDULE_WEATHER_TASK)
//                .one();
//
//        // 判断55分钟内没有执行过的话
//        if (scheduleTaskDO == null || scheduleTaskDO.getExecuteTime().isBefore(LocalDateTime.now().minusMinutes(55))) {
//            List<WeatherPointDO> weatherPointDOList = weatherPointDAO.lambdaQuery()
//                    .eq(WeatherPointDO::getDeleted, Constants.UN_DELETE_STATUS)
//                    .list();
//
//            // 异步调用请求
//            fetchAndSaveWeatherData(weatherPointDOList);
//
//            // 更新执行时间
//            updateExecutionTime(scheduleTaskDO);
//        } else {
//            System.out.println("任务在过去55分钟内已执行，无需再次执行。");
//        }
//        System.out.println("定时任务执行完成时间: " + new java.util.Date());
//    }
//
//    private void fetchAndSaveWeatherData(List<WeatherPointDO> weatherPointDOList) {
//        CompletionService<List<WeatherDO>> completionService = new ExecutorCompletionService<>(ThreadPool.commonPool);
//
//        for (WeatherPointDO weatherPointDO : weatherPointDOList) {
//            String position = weatherPointDO.getPosition();
//            String longitude = position.split(Constants.POSITION_DELIMITER)[0];
//            String latitude = position.split(Constants.POSITION_DELIMITER)[1];
//
//            completionService.submit(() -> getWeather(longitude, latitude, weatherApiKey));
//        }
//
//        for (int i = 0; i < weatherPointDOList.size(); i++) {
//            try {
//                List<WeatherDO> weatherDOS = completionService.take().get();
//                for (WeatherDO weatherDO : weatherDOS) {
//                    weatherDAO.saveOrUpdate(weatherDO,
//                            new UpdateWrapper<WeatherDO>()
//                                    .eq("position", weatherDO.getPosition())
//                                    .eq("datetime_epoch", weatherDO.getDatetimeEpoch()));
//                }
//            } catch (Exception e) {
//                log.error("异步任务执行异常: {}", e.getMessage(), e);
//            }
//        }
//    }
//
//    private void updateExecutionTime(ScheduleTaskDO scheduleTaskDO) {
//        if (scheduleTaskDO == null) {
//            scheduleTaskDO = new ScheduleTaskDO();
//            scheduleTaskDO.setTaskName(SCHEDULE_WEATHER_TASK);
//        }
//        scheduleTaskDO.setExecuteTime(LocalDateTime.now());
//        scheduleTaskDAO.saveOrUpdate(scheduleTaskDO);
//    }
//}